#include "RegisterDialog.h"
#include "ui_RegisterDialog.h"
#include "global.h"
#include <iostream>

#include "HttpMng.h"

RegisterDialog::RegisterDialog(QWidget* parent) : QWidget(parent), ui(new Ui::Register) {
    ui->setupUi(this);

    //设置密码为不显示
    ui->pass_lineEdit->setEchoMode(QLineEdit::Password);
    ui->repeat_lineEdit->setEchoMode(QLineEdit::Password);
    refresh(ui->tip_label);
    //注册对应信号的处理逻辑
    initHttpHandlers();
    connect(HttpMng::GetInstance().get(), &HttpMng::sig_reg_mod_finish, this, &RegisterDialog::slot_reg_mod_finish);
}

RegisterDialog::~RegisterDialog() {
    delete ui;
}

void RegisterDialog::on_get_code_btn_clicked() const {
    const auto email = ui->mail_lineEdit->text();
    const static QRegularExpression regular_expression(R"((\w+)(\.|_)?(\w*)@(\w+)(\.(\w+)+))");

    if (regular_expression.match(email).hasMatch()) {
        //发送http请求获取验证码
        QJsonObject json_obj;
        json_obj["email"] = email;
        HttpMng::GetInstance()->PostHttpReq(QUrl(gate_url_prefix + "/get_verify_code"),
                                            json_obj, ID_GET_VERIFY_CODE, REGISTER_MOD);
    }
    else {
        showTip(tr("邮箱地址不正确"), false);
    }
}

void RegisterDialog::slot_reg_mod_finish(const ReqId id, const QString& res, const ErrorCodes err) const {
    if (err != SUCCESS) {
        showTip(tr("网络请求错误"), false);
        return;
    }

    // 解析 JSON 字符串,res需转化为QByteArray
    const QJsonDocument jsonDoc = QJsonDocument::fromJson(res.toUtf8());
    //json解析错误
    if (jsonDoc.isNull() || !jsonDoc.isObject()) {
        showTip(tr("json解析错误"), false);
        return;
    }

    //调用对应的逻辑,根据id回调。
    _handlers[id](jsonDoc.object());
}

//点击注册界面确认按钮
void RegisterDialog::on_confirm_btn_clicked() const {
    if (ui->user_lineEdit->text() == "") {
        showTip(tr("用户名不能为空"), false);
        return;
    }
    if (ui->mail_lineEdit->text() == "") {
        showTip(tr("邮箱不能为空"), false);
        return;
    }
    if (ui->pass_lineEdit->text() == "") {
        showTip(tr("密码不能为空"), false);
        return;
    }
    if (ui->repeat_lineEdit->text() == "") {
        showTip(tr("确认密码不能为空"), false);
        return;
    }
    if (ui->repeat_lineEdit->text() != ui->pass_lineEdit->text()) {
        showTip(tr("密码和确认密码不匹配"), false);
        return;
    }
    if (ui->code_lineEdit->text() == "") {
        showTip(tr("验证码不能为空"), false);
        return;
    }

    QJsonObject json_obj;
    json_obj["user"] = ui->user_lineEdit->text();
    json_obj["email"] = ui->mail_lineEdit->text();
    json_obj["password"] = ui->pass_lineEdit->text();
    json_obj["confirm"] = ui->repeat_lineEdit->text();
    json_obj["verifyCode"] = ui->code_lineEdit->text();
    HttpMng::GetInstance()->PostHttpReq(QUrl(gate_url_prefix + "/user_register"),
                                        json_obj, ID_REG_USER, REGISTER_MOD);
}

void RegisterDialog::showTip(const QString& text, const bool is_ok) const {
    ui->tip_label->setText(text);

    if (is_ok) {
        ui->tip_label->setProperty("state", "normal");
    }
    else {
        ui->tip_label->setProperty("state", "error");
    }
    refresh(ui->tip_label);
}

void RegisterDialog::initHttpHandlers() {
    //获取验证码回掉逻辑
    _handlers.insert(ID_GET_VERIFY_CODE, [this](QJsonObject jsonObj) {
        const int error = jsonObj["error"].toInt();
        if (error != SUCCESS) {
            showTip(tr("参数错误"), false);
            return;
        }
        const auto email = jsonObj["email"].toString();
        showTip(tr("验证码已发送到邮箱，注意查收"), true);
        qDebug() << "email is " << email;
    });

    //注册用户
    _handlers.insert(ID_REG_USER, [this](QJsonObject jsonObj) {
        const int error = jsonObj["error"].toInt();
        if (error != SUCCESS) {
            showTip(tr("参数错误"), false);
            return;
        }
        const auto email = jsonObj["email"].toString();
        showTip(tr("用户注册成功"), true);
        qDebug() << "email is " << email;
    });
}
